package com.mttqq.family.action;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
/*******************************************************************************
 * <b>类名：BaseAction</b> <br/>
 * 功能：struts 基类<br/>
 * 日期: 2012-06-13 <br/>
 * 
 * Copyright: Beijing Uniits Technology Co.,Ltd.
 * 
 * Copyright (c) 2012 Company:北京广联易通信息技术有限公司
 * 
 * @author 谢广泉 xieguangquan@uniits.com
 * @version 1.0.0
 * 
 ******************************************************************************/
public class BaseAction extends ActionSupport implements SessionAware,RequestAware,CookiesAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * session 
	 */
	public Map<String, Object> session ;
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	/**
	 * session 
	 */
	public Map<String, Object> request ;
	
	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
		
	}
	
	/**
	 * cookie 
	 */
	public Map<String, String> cookie;
	
	@Override
	public void setCookiesMap(Map<String, String> cookie) {
		if(cookie != null && cookie.size() > 0 ){
			for (String key : cookie.keySet()) {
				try {
					
					key = URLEncoder.encode(key,"UTF-8");
					String value = URLEncoder.encode(cookie.get(key),"UTF-8");
					
					Cookie cookies = new Cookie(key,value);
					cookies.setPath("/");//cookie的保存路径，/同一应用内都可读取该cookie，或指定path仅限该path能读取cookie  
					cookies.setMaxAge(30*60);    //有效期，单位为秒  
					ServletActionContext.getResponse().addCookie(cookies); //生成cookie到客户端  
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
			
			this.cookie = cookie;
			
		}
	}
	
	public static String md5(String s)  {
		String md5_str = "";
		try {

			MessageDigest m = MessageDigest.getInstance("MD5");
			m.update(s.getBytes(), 0, s.length());
			md5_str = new BigInteger(1, m.digest()).toString(16);

			if (md5_str.length() < 32) {
				md5_str = "0".concat(md5_str);
			}

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return md5_str;
	}
	

	
	public Map<String, String> getCookiesMap(){
		Cookie[] cookies = ServletActionContext.getRequest().getCookies(); //生成cookie到客户端  
		Map<String, String> map = null;
		if(cookies != null){
			map = new HashMap<String,String>();
			for (Cookie cookie : cookies) {
				
				try {
					String key  = URLEncoder.encode(cookie.getName(),"UTF-8");
					String value = URLEncoder.encode(cookie.getValue(),"UTF-8");
					
					map.put(key, value);
					
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				
			}
			
			this.cookie  = map;
		}
		
		return map;
		
	}
	
	/**
	 * json
	 */
	public String jsonresult;
	
	public void setJsonresult(String json){
		this.jsonresult = json;
	}
	
	/**
	 * 非空认证
	 * @param value
	 * @return
	 */
	public boolean isInvalid(String value) {
	    return (value == null) || (value.length() == 0);
	}
	/**
	 * 非空认证
	 * @param value
	 * @return
	 */
	public boolean isInvalid(int value) {
	    return (value == 0);
	}
	/**
	 * 非空认证
	 * @param value
	 * @return
	 */
	public boolean isInvalid(Date value) {
		return (value == null);
	}

	

	

}
